﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core.ServiceModel;

namespace Baosight.ZNH.ZMCP.UI
{
    public partial class ZMCP10 : EFViewCmes
    {
        private EFTreeItem item;    //记录选中的树节点
        public ZMCP10()
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(ZMCMD0_Loaded);
        }
        /// <summary>
        /// 页面加载事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ZMCMD0_Loaded(object sender, RoutedEventArgs e)
        {
            this.purchasePlanTree.Refresh();

        }
        /// <summary>
        /// 选中树节点，导航右侧的页面
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void purchasePlanTree_Selected(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            item = this.purchasePlanTree.SelectedItems[0] as EFTreeItem;
            Dictionary<string, string> args = this.GetTreeNodeInfo(item);
            if (item != null)
            {
                if (item.IsLeaf)
                {
                    this.container.LoadView<EFCustomViewContainerPanel>("ZMCP12", args);
                }
                else
                {
                    this.container.LoadView<EFCustomViewContainerPanel>("ZMCP11", args);
                }
            }
        }

        /// <summary>
        /// //获取tree选中的结点对应的label
        /// </summary>
        /// <param name="tree"></param>
        /// <returns></returns>
        private Dictionary<string, string> GetTreeNodeInfo(EFTreeItem item)
        {
            Dictionary<string, string> args = new Dictionary<string, string>();
            string fdPlanYear = string.Empty;
            string fdPlanMonth = string.Empty; ;
            if (item.IsLeaf)
            {
                //月份为叶子，其值为年月，类似20112
                string Itemlabel = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
                fdPlanYear = Itemlabel.Substring(0, 4);
                fdPlanMonth = Itemlabel.Substring(4) ;
            }
            else
            {
                fdPlanYear = item.ItemObject.GetType().GetProperty("label").GetValue(item.ItemObject, null) as string;
            }
            args.Add("fdPlanYear", fdPlanYear);
            args.Add("fdPlanMonth", fdPlanMonth);

            return args;
        }

        /// <summary>
        /// 展开树节点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void purchasePlanTree_ItemPrepared(object sender, Telerik.Windows.Controls.RadTreeViewItemPreparedEventArgs e)
        {
            if (e.PreparedItem == null)
            {
                return;
            }
            EFTreeItem treeItem = e.PreparedItem as EFTreeItem;
            string label = treeItem.ItemObject.GetType().GetProperty("label").GetValue(treeItem.ItemObject, null) as string;
            String date = DateTime.Today.ToString("yyyy");

            if (treeItem.Level == 0 && label.Equals(date))
            {
                this.purchasePlanTree.SelectedItem = treeItem;//选中
                treeItem.ExpandItem();//展开
            }
        }


    }
}
